package com.ives.launcher.view;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;

import com.ives.launcher.MainActivity;
import com.ives.launcher.logic.DragContainer;
import com.ives.launcher.logic.DragController;
import com.ives.launcher.model.ItemInfo_App;
import com.ives.launcher.util.ImageTool;

/**
 * 抽屉中的容器，用来装所有已安装应用的GridView
 * 实现DragContainerSource
 * @author ives
 * 
 */
public class View_SlidingDrawer_AllApsGridView extends GridView implements
		AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener,DragContainer{
	
	private MainActivity mainActivity;
	private DragController mDragger;

	public View_SlidingDrawer_AllApsGridView(Context context) {
		super(context);
	}

	public View_SlidingDrawer_AllApsGridView(Context context, AttributeSet attrs) {
		this(context, attrs, android.R.attr.gridViewStyle);
	}

	public View_SlidingDrawer_AllApsGridView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.setBackgroundColor(Color.BLACK);
		//this.setNumColumns(AUTO_FIT);
		this.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
	}

	/**
	 * 当装载完毕的时候调用
	 */
	@Override
	protected void onFinishInflate() {
		setOnItemClickListener(this);
		setOnItemLongClickListener(this);
	}

	/**
	 * 启动某个应用
	 */
	@Override
	public void onItemClick(AdapterView parent, View v, int position, long id) {
		ItemInfo_App app = (ItemInfo_App) ((View_Cell)v).getItemInfo();
		mainActivity.startActivity(app.intent);
	}

	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view,int position, long id) {
		if (!view.isInTouchMode()) {// 如果不再触摸模式，返回
			return false;
		}
		// 取得被触摸的应用
		ItemInfo_App app = (ItemInfo_App) parent.getItemAtPosition(position);
		app = new ItemInfo_App(app);
		// 发起一个拖动动作
		mDragger.startDrag(view, this,View_Root.TOUCH_TYPE_DRAG_COPY);
		mainActivity.closeSlidingDrawer();
		return true;
	}

	/**
	 * 设置拖动控制器
	 * @param dragger
	 */
	public void setDragger(DragController dragger) {
		mDragger = dragger;
	}

	public void setMainActivity(MainActivity mainActivity) {
		this.mainActivity = mainActivity;
	}

	@Override
	public void onDragIn(View view, DragContainer source) {
		
	}

	@Override
	public void onDropOut(View view, DragContainer target) {
		
	}

	@Override
	public void onDropCompleted(View target, boolean success) {
		
	}

	@Override
	public boolean disposeDrop(MotionEvent event,View_Drag drag) {
		return false;
	}

	@Override
	public int[] findDropCellFinalPosition(int touchX, int touchY,
			View_Drag drag) {
		return null;
	}

	@Override
	public boolean moveToPosition(int x1,int y1,View_Drag drag) {
		
		return false;
	}

	@Override
	public void setPositionStatus(int x, int y, int w, int h, boolean used) {
		
	}

	@Override
	public void removeCell(View_Cell cell) {
		
	}
	@Override
	public View getChildByPosition(int x,int y){
		return null;
	}
	public static class MyAdapter extends BaseAdapter{
		ArrayList<ItemInfo_App> apps;
		Activity activity;
		
		public MyAdapter(Activity activity,ArrayList<ItemInfo_App> apps) {
			this.activity = activity;
			this.apps = apps;
		}

		@Override
		public int getCount() {
			return apps.size();
		}

		@Override
		public Object getItem(int position) {
			return apps.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position/4;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
	        	final ItemInfo_App info = (ItemInfo_App) getItem(position);
	            convertView =  new View_Cell(activity,info.title.toString(),info.icon, 28f, 2f);
	            convertView.setLayoutParams(new AbsListView.LayoutParams(parent.getHeight()/6,parent.getWidth()/4));
	            View_Cell cell  = ((View_Cell)convertView);
	            cell.setItemInfo(info);
	        }
	        return convertView;
		}
		
	}
	@Override
	public boolean checkCanMoveToPosition(int x, int y, View_Drag drag) {
		// TODO Auto-generated method stub
		return false;
	}


}